﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using nr.Tombola.Cards;

namespace nr.Tombola.Controls
{
    public partial class CardView : UserControl
    {
        private Card card;

        public Card Card
        {
            get { return card; }
            set
            {
                if (card != value)
                {
                    layout.SuspendLayout();
                    ClearCard();
                    if (value != null)
                    {
                        for (int row = 0; row < 3; ++row)
                            for (int dec = 0; dec < 9; ++dec)
                            {
                                if (value[row][dec] > 0)
                                {
                                    TombolaNumber number = new TombolaNumber();
                                    number.Name = string.Format("number{0}_{1}", row, dec);
                                    number.Number = value[row][dec];
                                    number.Dock = DockStyle.Fill;
                                    number.Extract = false;
                                    number.Click += number_Click;

                                    layout.Controls.Add(number, dec, row);
                                }
                            }
                    }
                    layout.ResumeLayout();
                    card = value;
                }
            }
        }

        void number_Click(object sender, EventArgs e)
        {
            TombolaNumber ctrl = (TombolaNumber)sender;
            ctrl.Extract = !ctrl.Extract;
        }

        public void Purge()
        {
            SuspendLayout();
            for (int row = 0; row < 3; ++row)
                for (int dec = 0; dec < 9; ++dec)
                {
                    if (Card[row][dec] > 0)
                    {
                        TombolaNumber number = (TombolaNumber)layout.Controls.Find(string.Format("number{0}_{1}", row, dec), false)[0];
                        number.Extract = false;
                    }
                }
            ResumeLayout();
        }

        public void TurnNumber(int number, bool on)
        {
            for (int row = 0; row < 3; ++row)
                for (int dec = 0; dec < 9; ++dec)
                {
                    if (Card[row][dec] > 0)
                    {
                        TombolaNumber n = (TombolaNumber)layout.Controls.Find(string.Format("number{0}_{1}", row, dec), false)[0];
                        if (n.Number == number) n.Extract = on;
                    }
                }
        }

        private void ClearCard()
        {
            layout.Controls.Clear();
        }

        public CardView()
        {
            card = null;

            InitializeComponent();
        }
    }
}
